package orchestration.utils;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import android.net.Uri;

import orchestration.businessprocess.model.BPNodeId;
import orchestration.businessprocess.model.nodes.BPNode;
import orchestration.persistence.exceptions.ConversionException;

public class ConversionUtils {

	public static String[] long2StringArray(long l) {
		String[] whereArgs= new String[1];
		whereArgs[0]=""+l;
		return whereArgs;
	}

	public static String set2string(Set<BPNode> visited) {
		String result="";
		for(BPNode node : visited)
			result+=node.getId().getValue()+ " ";
		return result;
	}

	public static String list2string(ArrayList<BPNode> nodes) {
		String result="";
		for(BPNode node : nodes)
			result+= node.getId().getValue()+" ";
		return result;
	}
	public static Object byteArray2Object(byte[] encoded) throws ConversionException {
		Object result=null;
		ByteArrayInputStream bis = new ByteArrayInputStream(encoded);
		try {
			ObjectInputStream obj_in = new ObjectInputStream(bis);

			result =  obj_in.readObject();
		} catch (IOException e) {
			throw new ConversionException(e);
		} catch (ClassNotFoundException e) {
			throw new ConversionException(e);
		}
		return result;
	}

	public static byte[] object2ByteArray(Serializable s) throws ConversionException{
		ByteArrayOutputStream bos = null;
		try {
			
		        bos = new ByteArrayOutputStream();
		        ObjectOutputStream obj_out = new ObjectOutputStream(bos);
		        obj_out.writeObject(s);
		} catch (IOException e) {
		    	throw new ConversionException(e);
		}
		return bos.toByteArray();
	}

	public static ArrayList<BPNodeId> stringArray2NodeIdList(
			String[] toVisitStringArray) {
		ArrayList<BPNodeId> toVisitIds = new ArrayList<BPNodeId>();
		for(int i=toVisitStringArray.length-1;i>=0;i--)
			toVisitIds.add(new BPNodeId(toVisitStringArray[i]));
		return toVisitIds;
	}

	public static HashSet<BPNodeId> stringArray2NodeIdSet(String[] visitedStringArray) {
		HashSet<BPNodeId> visitedIds= new HashSet<BPNodeId>();
		for(String s : visitedStringArray)
			visitedIds.add(new BPNodeId(s));
		return visitedIds;
	}

	public static Uri[] stringArray2UriArray(String[] strArr) {
		Uri[] uriArr;
		int length = strArr.length;
		uriArr= new Uri[length];
		for(int i=0;i<length;i++)
			uriArr[i]=Uri.parse(strArr[i]);
		return uriArr;
	}

	public static String[] uriArray2StringArray(Uri[] uriArray) {
		int length = uriArray.length;
		String[] stringArray=new String[length];
		for(int i=0;i<length;i++)
			stringArray[i]=uriArray[i].toString();
		return stringArray;
	}

	public static ArrayList<String> uriList2StringList(List<Uri> value) {
		ArrayList<String> result= new ArrayList<String>();
		for(Uri u : value)
			result.add(u.toString());
		return result;
	}

	public static List<Uri> stringList2UriList(List<String> value) {
		List<Uri> result= new ArrayList<Uri>();
		for(String u : value)
			result.add(Uri.parse(u));
		return result;
	}
}
